/*
 *所有关于ShipmentsModel类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*ShipmentsModelService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using MyMES.Dto;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels.Core;
using System.Threading.Tasks;
using System;

namespace MyMES.Services
{
    public partial class ShipmentsModelService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<ShipmentsModel> _shipmentRepo;
        private readonly IBaseRepository<Customer> _customerRepo;
        private readonly IShipmentsModelRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public ShipmentsModelService(
            IShipmentsModelRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<ShipmentsModel> shipmentRepo,
            IBaseRepository<Customer> customerRepo
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _shipmentRepo = shipmentRepo;
            _customerRepo = customerRepo;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        
        /// <summary>
        /// 获取发货通知单列表
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns>分页后的发货通知单列表</returns>
        public async Task<PageGridData<ShipmentsModelDTO>> GetShipmentsModel(GetShipmentsDTO dto)
        {
            PageGridData<ShipmentsModelDTO> pageData = new PageGridData<ShipmentsModelDTO>();
            
            var shipmentsModelList = from a in _shipmentRepo.GetAll()
                                    join c in _customerRepo.GetAll() on a.CustCode equals c.CustCode into customerTemp
                                    from customer in customerTemp.DefaultIfEmpty()
                                    select new ShipmentsModelDTO()
                                    {
                                        Id = a.Id,
                                        ShipmentsCode = a.ShipmentsCode,
                                        ShipmentsName = a.ShipmentsName,
                                        SaleCode = a.SaleCode,
                                        CustCode = a.CustCode,
                                        ShipmentsTime = a.ShipmentsTime,
                                        ReceivingName = a.ReceivingName,
                                        Phone = a.Phone,
                                        ReceivingAddress = a.ReceivingAddress,
                                        Speak = a.Speak,
                                        State = a.State,
                                        CustName = customer.CustName,
                                        CustAbbreviation = customer.CustAbbreviation
                                    };
            
            // 根据条件筛选数据
            shipmentsModelList = shipmentsModelList.WhereIF(!string.IsNullOrEmpty(dto.ShipmentsCode),
                                x => x.ShipmentsCode.Contains(dto.ShipmentsCode));
            
            shipmentsModelList = shipmentsModelList.WhereIF(!string.IsNullOrEmpty(dto.ShipmentsName),
                                x => x.ShipmentsName.Contains(dto.ShipmentsName));
            
            shipmentsModelList = shipmentsModelList.WhereIF(!string.IsNullOrEmpty(dto.SaleCode),
                                x => x.SaleCode.Contains(dto.SaleCode));
            
            shipmentsModelList = shipmentsModelList.WhereIF(!string.IsNullOrEmpty(dto.CustCode),
                                x => x.CustCode.Contains(dto.CustCode));
                                
            shipmentsModelList = shipmentsModelList.WhereIF(!string.IsNullOrEmpty(dto.CustName),
                                x => x.CustName.Contains(dto.CustName));
                                
            shipmentsModelList = shipmentsModelList.WhereIF(!string.IsNullOrEmpty(dto.CustAbbreviation),
                                x => x.CustAbbreviation.Contains(dto.CustAbbreviation));
            
            // 计算总记录数
            pageData.total = await shipmentsModelList.CountAsync();
            
            // 分页并排序
            var data = await shipmentsModelList.OrderByDescending(a => a.ShipmentsTime)
                            .Skip((dto.PageIndex - 1) * dto.PageSize)
                            .Take(dto.PageSize)
                            .ToListAsync();
            
            pageData.rows = data;
            
            return pageData;
        }

        /// <summary>
        /// 添加发货通知单
        /// </summary>
        /// <param name="shipmentsModel">发货通知单实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> AddShipmentsModel(ShipmentsModel shipmentsModel)
        {
            var res = new WebResponseContent();
            
            var i = await _shipmentRepo.AddAsync(shipmentsModel);
            
            res.OK("新增成功");
            
            return await Task.FromResult(res);
        }

        /// <summary>
        /// 更新发货通知单
        /// </summary>
        /// <param name="shipmentsModel">发货通知单实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> UpdShipmentsModel(ShipmentsModel shipmentsModel)
        {
            var res = new WebResponseContent();
            
            var i = await _shipmentRepo.UpdateAsync(shipmentsModel);
            
            res.OK("修改成功");
            
            return await Task.FromResult(res);
        }
        
        /// <summary>
        /// 删除发货通知单
        /// </summary>
        /// <param name="shipmentsCode">发货通知单编号</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> RemShipmentsModel(string shipmentsCode)
        {
            var res = new WebResponseContent();
            
            var i = await _shipmentRepo.DelAll(x => x.ShipmentsCode.Contains(shipmentsCode));
            
            res.OK("删除成功");
            
            return await Task.FromResult(res);
        }
        
        /// <summary>
        /// 查找发货通知单
        /// </summary>
        /// <param name="shipmentsCode">发货通知单编号</param>
        /// <returns>操作结果</returns>
        public Task<WebResponseContent> FindShipmentsModel(string shipmentsCode)
        {
            var res = new WebResponseContent();
            
            var shipmentsModelList = _shipmentRepo.GetAll(x => x.ShipmentsCode == shipmentsCode).FirstOrDefault();
            
            res.OK("查询成功", shipmentsModelList);
            
            return Task.FromResult(res);
        }
    }
}
